<?php
/**
* This is a tool class to add unit tests scenario to webappkit kits
* @use Simpletest kit
* @author J.Ducastel <jeremie@ducastel.name>
*
*/
class SimpleTestTool extends WakTool {

    /**
    * @static
    */
    function toolFactory($kit,$cfg) {
        return new SimpleTestTool($kit,$cfg);
    }

    /**
    * runs kit tests
    * @access public
    * @return WakTestReporter
    */
    function run() {
        // loading simple test framework
        // $ST=Webappkit::getKit('simpletest');
		// building test reporter
		$this->reporter=new WakTestReporter();
		// preparing test environment
		$this->setUp();

        // running test classes
		$this->runSelfClasses($this->cfg['classes']);

		//else // running all existing test classes
		//	$this->runAllClasses();

		// cleaning environment after tests
		$this->tearDown();
        // print_r($this->reporter);
        return $this->reporter;
    }

    /**
    * this function is ran before tests
    */
	function setUp() {

	}

    /**
    * this function is ran after tests
    */
	function tearDown() {

	}

    /**
    * @var array config data
    */
    var $cfg=array(
        'label'=>'Anonymous test scenario tool'
        ,'classes'=>array()
        );

	/**
	 * run self test classes
     * those are defined in $cfg->classes as class => file
     * @return bool
	 */
	function runSelfClasses() {
        if (!$this->cfg['classes'])
            return false;
		$group=new GroupTest("Tests for {$this->kit}");
        $path=Webappkit::getPathFromId($this->kit);
		foreach ($this->cfg['classes'] as $class => $file) {
            // loading class def
            Webappkit::loadLibFile($path.$file);
			if (!class_exists($class)) {
                trigger_error("class $class not found in $path$file for tests");
                continue;
				//return false;
            }
			// adding class to group test
			$group->addTestClass($class);
		}
		// running group test
        // $this->reporter=new SimpleReporter();
		return $group->run($this->reporter);
        // print_r($reporter);
	}
}
